var buttonActivate = function() {
	// popup edit window and fill with post content
	$(".edit_btn").click(function(e) {
		// cancel the link behavior
		e.preventDefault();
		
		$(this).colorbox({inline:true, width:"50%"});
			
		//get the id of blog to be edit
		var blog_id = $(this).attr('rel');
		
		// get the author, title, and content
		//var author = $("#" + blog_id).find(".author").find("span").html();
		var author = currentUser;
		var title = $("#" + blog_id).find(".title").html();
		var content = "";
		for(var i in blogposts) {
			if(blogposts[i].id == blog_id) {
				content = blogposts[i].content;
				break;
			}
		}
		//var content = $("#" + blog_id).find(".content").html();
		
		$("#edit_id").val(blog_id);
		$("#edit_user").val(author);
		$("#edit_title").val(title);
		$("#edit_content").val(content);
		
	});
	
	// popup delete window
	$(".delete_btn").click(function(e) {
		// cancel the link behavior
		e.preventDefault();
	
		$(this).colorbox({inline:true, width:"50%"});
		
		//get value of id and author
		var blog_id = $(this).attr('rel');
		var author = currentUser;

		// fill in the delete form
		$("#delete_id").val(blog_id);
		$("#delete_user").val(author);
	
	});
	
	/* scroll to a post when click on a list in bloglist
	$(".bloglist_link").click(function(e) {
		e.preventDefault();
		// scroll to the post position
		$("html,body").animate({scrollTop: $(this.attribute("href")).offset().top}, 'fast');
	}) */;
	
	
}

var eventHandler = function() {
	
	/*
	 * create blog related
	 */
	
	// popup create form
	$("#create").click(function(e) {
		
		// cancel the link behavior
		e.preventDefault();		
		$(this).colorbox({inline:true, width:"50%"});		
	});
	
	
	// submit create - parse form values to the ajax function createPost
	$("#create_submit").click(function(e) {
		
		// prevent default action
		e.preventDefault();		
		// get value from the form inputs
		var author = $("#create_user").val();
		var password = $("#create_pw").val();
		var title = $("#create_title").val();
		var content = $("#create_content").val();		
		createPost(author, password, title, content);
	});
	
	
	// cancel create - clear form value
	$("#create_cancel").click(function() {
		$("#create_user").val("");
		$("#create_pw").val("");
		$("#create_title").val("");
		$("#create_content").val("");		
	});
	
	
	/*
	 * edit blog related
	 */
	
	// submit edit - parse form values to the ajax function editPost
	$("#edit_submit").click(function(e) {
		
		// prevent default action
		e.preventDefault();		
		// get value from the form inputs
		var author = currentUser;
		var password = $("#edit_pw").val();
		var title = $("#edit_title").val();
		var content = $("#edit_content").val();
		var id = $("#edit_id").val();
		editPost(author, password, title, content, id);		
	});
	
	// cancel edit
	$("#edit_cancel").click(function() {
		$.colorbox.close();
	});
	
	
	/*
	 * delete blog related
	 */
	
	// submit delete - parse form values to the ajax function deletePost
	$("#delete_submit").click(function(e) {
		
		// prevent default action
		e.preventDefault();		
		// get value from the form inputs
		var author = $("#delete_user").val();
		var password = $("#delete_pw").val(); 
		var id = $("#delete_id").val();		
		deletePost(author, password, id);		
	});
	
	// cancel delete
	$("#delete_cancel").click(function() {
		$.colorbox.close();
	});
	
	
	/*
	 * blog user related
	 */
	
	// when select a user, clean the page and show content accordingly
	$("#blog_users").change(function(){
		
		// clear up the global variable
		blogposts = [];		
		// change currentUser
		currentUser = $('#blog_users').val();
		getPosts(currentUser);		
	});
	
	
	/*
	 * calendar related
	 */
	
	// calendar - return to all posts
	$("#return").click(function(e){
		
		// prevent default action
		e.preventDefault();		
		getPosts(currentUser);		
	});

};
